package org.sjsu.freedoor.util;

import javax.ws.rs.core.MediaType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public final class Utilities {
	
	public static JSONObject toJsonObject(String str)
	{
		JSONObject obj;
		try {
			obj = new JSONObject(str);
		} catch (JSONException e) {
			obj = null;
			e.printStackTrace();
		}
		
		return obj;
	}
	
	public static JSONArray toJsonArray(String str)
	{
		JSONArray arr;
		try {
			arr = new JSONArray(str);
		} catch (JSONException e) {
			arr = null;
			e.printStackTrace();
		}
		
		return arr;
	}
	
	public static String sendGetRequest(String url) {
		Client client = Client.create();
		WebResource webResource = client.resource(url);
		String s = webResource.get(String.class);
		return s;
	}
	
	public static String sendPutRequest(String url, JSONObject obj) {
		Client client = Client.create();
		WebResource webResource = client.resource(url);
		String s = webResource.header("session-token", "theadjs")
		.entity(obj, MediaType.APPLICATION_JSON)
		.put(String.class);
		return s;
	}

	public static String sendDeleteRequest(String url) {
		Client client = Client.create();
		WebResource webResource = client.resource(url);
		String s = webResource.header("session-token", "theadjs")
		.delete(String.class);
		return s;
	}
}
